package cn.iocoder.yudao.module.wine.service.membertype;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.wine.controller.app.membertype.vo.AppMemberTypePageReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.wine.controller.admin.membertype.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.membertype.MemberTypeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.membertype.MemberTypeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 会员类型信息 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class MemberTypeServiceImpl implements MemberTypeService {

    @Resource
    private MemberTypeMapper memberTypeMapper;

    @Override
    public Long createMemberType(MemberTypeSaveReqVO createReqVO) {
        // 插入
        MemberTypeDO memberType = BeanUtils.toBean(createReqVO, MemberTypeDO.class);
        memberTypeMapper.insert(memberType);
        // 返回
        return memberType.getId();
    }

    @Override
    public void updateMemberType(MemberTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateMemberTypeExists(updateReqVO.getId());
        // 更新
        MemberTypeDO updateObj = BeanUtils.toBean(updateReqVO, MemberTypeDO.class);
        memberTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberType(Long id) {
        // 校验存在
        validateMemberTypeExists(id);
        // 删除
        memberTypeMapper.deleteById(id);
    }

    private void validateMemberTypeExists(Long id) {
        if (memberTypeMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public MemberTypeDO getMemberType(Long id) {
        return memberTypeMapper.selectById(id);
    }

    @Override
    public PageResult<MemberTypeDO> getMemberTypePage(MemberTypePageReqVO pageReqVO) {
        return memberTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MemberTypeDO> getMemberTypeList(AppMemberTypePageReqVO pageReqVO) {

        return memberTypeMapper.selectList(new LambdaQueryWrapperX<MemberTypeDO>()
                .likeIfPresent(MemberTypeDO::getName, pageReqVO.getName())
                .eqIfPresent(MemberTypeDO::getPrice, pageReqVO.getPrice())
                .betweenIfPresent(MemberTypeDO::getCreateTime, pageReqVO.getCreateTime())
                .orderByDesc(MemberTypeDO::getId));
    }

}
